<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/27
 * Time: 3:47 PM
 */
namespace app\common\models;

class Role extends BaseModel
{
    public $table = 'yq_role';
    public $timestamps = true;
    protected $guarded = [''];

    const ROLE_FOUNDER_STR = 'founder';//站点创建人

    //
    public function permissions()
    {
        return $this->belongsToMany(Permission::class,'yq_permission_role','role_id','permission_id');
    }
    public function roleUser()
    {
        return $this->hasMany(UserRole::class, 'role_id');
    }
    public function rolePermission()
    {
        return $this->hasMany(Permission::class, 'item_id');
    }
    public static function getRoleById($roleId)
    {
        return static::where('id', $roleId)
            ->with(['rolePermission' => function($query) {
                return $query->select('id', 'item_id','permission')->where('type', '=', Permission::TYPE_ROLE);
            }])
            ->first();
    }
    public static function deleteRole($roleId)
    {
        return static::where('id', $roleId)->delete();
    }
    public  function atributeNames() {
        return [
            'name'  => '角色名称'
        ];
    }

    public  function rules()
    {
        return [
            'name' => 'required'
        ];
    }
}